<?php

namespace App\Repositories;

use App\Models\UserTicketModel;
use InfyOm\Generator\Common\BaseRepository;

/**
 * Class UserTicketModelRepository
 * @package App\Repositories
 * @version May 9, 2018, 1:33 pm UTC
 *
 * @method UserTicketModel findWithoutFail($id, $columns = ['*'])
 * @method UserTicketModel find($id, $columns = ['*'])
 * @method UserTicketModel first($columns = ['*'])
*/
class UserTicketRepository extends BaseRepository
{
    /**
     * Configure the Model
     **/
    public function model()
    {
        return UserTicketModel::class;
    }

    /**
     * @param $userId
     * @param $ticketId
     * @return $num int
     */
    public function getUserTicketCount($userId, $ticketId)
    {
        $num = UserTicketModel::where('user_id', $userId)
            ->where('ticket_id', $ticketId)
            ->where('status', 0)
            ->count();

        return $num;
    }

    public function getUserTicket($userId, $ticketId)
    {
        $arr = UserTicketModel::where('user_id', $userId)
            ->where('ticket_id', $ticketId)
            ->where('status', 0)
            ->get()
            ->toArray();

        return $arr;
    }

    /**
     * 查询用户的代金券
     * @param $searchOptions
     * @param $sortOptions
     * @return mixed
     */
    public function search($searchOptions, $sortOptions)
    {
        $qb = (new UserTicketModel)->getQueryBySearchOptions($searchOptions);

        $count = $qb->count();

        $lists = UserTicketModel::setPage($qb, $sortOptions);

        $lists['total'] = $count;

        return $lists;
    }

    public function backstageSearch($searchOptions, $sortOptions)
    {
        $qb = (new UserTicketModel)->getQueryBySearchOptionsBackstage($searchOptions);

        $count = $qb->count();

        $lists = UserTicketModel::setPage($qb, $sortOptions);

        $lists['total'] = $count;

        return $lists;
    }
}
